package com.qys.livesMall.open.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.esotericsoftware.minlog.Log;
import com.qys.livesMall.open.enums.PushMessageTypeEnum;
import com.qys.livesMall.open.mapper.H2OpenSysMessageReadLogMapper;
import com.qys.livesMall.open.modal.dto.mail.PushAllDeviceDTO;
import com.qys.livesMall.open.modal.dto.mail.PushUserDeviceDTO;
import com.qys.livesMall.open.modal.entity.H2AppUserDevice;
import com.qys.livesMall.open.modal.entity.H2OpenSysMessage;
import com.qys.livesMall.open.modal.entity.H2OpenSysMessageReadLog;
import com.qys.livesMall.open.service.H2AppUserDeviceService;
import com.qys.livesMall.open.service.H2OpenSysMessageReadLogService;
import com.qys.livesMall.open.service.H2OpenSysMessageService;
import com.qys.livesMall.open.service.PushUserDeviceService;
import com.qys.livesMall.open.uniapp.PushReqBean;
import com.qys.livesMall.open.uniapp.PushUtil;
import com.qys.livesMall.open.uniapp.Result;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PushUserDeviceServiceImpl implements PushUserDeviceService {
    @Resource
    private H2AppUserDeviceService h2AppUserDeviceService;
    @Resource
    private PushUtil pushUtil;
    @Resource
    private H2OpenSysMessageService h2OpenSysMessageService;
    @Resource
    private H2OpenSysMessageReadLogService h2OpenSysMessageReadLogService;
    @Resource
    private H2OpenSysMessageReadLogMapper h2OpenSysMessageReadLogMapper;
    /**
     * 推送给指定用户群
     * @param pushUserDeviceDTO
     * @return
     */
    @Override
    public Result pushUserDevice(PushUserDeviceDTO pushUserDeviceDTO) {
        if (CollectionUtils.isEmpty(pushUserDeviceDTO.getUserIds())){
            return null;
        }
        List<H2AppUserDevice> h2AppUserDevices= h2AppUserDeviceService.getByIds(pushUserDeviceDTO.getUserIds(),pushUserDeviceDTO.getContent().getType().getCode());
        Result result=null;
        if (CollectionUtils.isNotEmpty(h2AppUserDevices)){
            List<String> deviceList=h2AppUserDevices.stream().map(H2AppUserDevice::getDeviceId).collect(Collectors.toList());
            String devices= StringUtils.join(deviceList,",");
            PushReqBean pushReqBean=new PushReqBean();
            pushReqBean.setTitle(JSONUtil.parseObj(pushUserDeviceDTO.getTitle()).toString());
            JSONObject jsonObject= JSONUtil.parseObj(pushUserDeviceDTO.getContent());
            pushReqBean.setContent(jsonObject.toString());
            //cid
            pushReqBean.setUser(devices);
            //消息
            result=pushUtil.pushMessageToCid(pushReqBean);
        }
        //系统消息日志
        H2OpenSysMessage h2OpenSysMessage=new H2OpenSysMessage();
        h2OpenSysMessage.setMessageType(0);
        h2OpenSysMessage.setCreator("system");
        h2OpenSysMessage.setCreateTime(LocalDateTime.now());
        h2OpenSysMessage.setSource(pushUserDeviceDTO.getContent().getType().getCode());
        h2OpenSysMessage.setTitle(JSONUtil.parseObj(pushUserDeviceDTO.getTitle()).toString());
        JSONObject jsonObject= JSONUtil.parseObj(pushUserDeviceDTO.getContent());
        h2OpenSysMessage.setContent(jsonObject.toString());
        h2OpenSysMessage.setReceiveType(1);
        h2OpenSysMessageService.save(h2OpenSysMessage);
         List<H2OpenSysMessageReadLog> messageReadLogs=new ArrayList<>();
        for (Long userid:pushUserDeviceDTO.getUserIds()){
            H2OpenSysMessageReadLog h2OpenSysMessageReadLog=new H2OpenSysMessageReadLog();
            h2OpenSysMessageReadLog.setMessageId(h2OpenSysMessage.getId());
            h2OpenSysMessageReadLog.setUserId(userid);
            h2OpenSysMessageReadLog.setMessageState(0);
            messageReadLogs.add(h2OpenSysMessageReadLog);
        }
        h2OpenSysMessageReadLogService.saveBatch(messageReadLogs);
        return result;
    }

    @Override
    public Result pushAllUserDevice(PushAllDeviceDTO pushAllDeviceDTO) {
        PushReqBean pushReqBean=new PushReqBean();
        pushReqBean.setTitle(JSONUtil.parseObj(pushAllDeviceDTO.getTitle()).toString());
        JSONObject jsonObject= JSONUtil.parseObj(pushAllDeviceDTO.getContent());
        pushReqBean.setContent(jsonObject.toString());
        Result result=pushUtil.pushMessageToAll(pushReqBean);
        //系统消息日志
        H2OpenSysMessage h2OpenSysMessage=new H2OpenSysMessage();
        h2OpenSysMessage.setCreator("system");
        h2OpenSysMessage.setCreateTime(LocalDateTime.now());
        h2OpenSysMessage.setMessageType(0);
        h2OpenSysMessage.setSource(pushAllDeviceDTO.getContent().getType().getCode());
        h2OpenSysMessage.setTitle(JSONUtil.parseObj(pushAllDeviceDTO.getTitle()).toString());
        h2OpenSysMessage.setContent(jsonObject.toString());
        h2OpenSysMessage.setReceiveType(0);
        h2OpenSysMessageService.save(h2OpenSysMessage);
        List<Long> userids=h2OpenSysMessageReadLogMapper.getAllAppuser();
        List<H2OpenSysMessageReadLog> messageReadLogs=new ArrayList<>();
        for (Long userid:userids){
            H2OpenSysMessageReadLog h2OpenSysMessageReadLog=new H2OpenSysMessageReadLog();
            h2OpenSysMessageReadLog.setMessageId(h2OpenSysMessage.getId());
            h2OpenSysMessageReadLog.setUserId(userid);
            h2OpenSysMessageReadLog.setMessageState(0);
            messageReadLogs.add(h2OpenSysMessageReadLog);
        }
        h2OpenSysMessageReadLogService.saveBatch(messageReadLogs);
        return result;
    }
}
